<template>
    <div class="app-container">
       <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="90px">
          <el-form-item label="发送者ID：" prop="sendID">
             <el-input
                v-model="queryParams.sendID"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item label="接收者ID：" prop="recvID">
             <el-input
                v-model="queryParams.recvID"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item label="会话类型：" prop="sessionType">
            <el-select v-model="queryParams.sessionType" placeholder="请选择" style="width: 240px">
                <el-option
                    v-for="item in sessionTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                >
                </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="消息类型：" prop="contentType">
            <el-select v-model="queryParams.contentType" placeholder="请选择" style="width: 240px">
                <el-option
                    v-for="item in contentTypeOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                >
                </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="发送时间:" prop="sendTime">
                <el-date-picker v-model="queryParams.sendTime"  value-format="YYYY-MM-DD" type="date" placeholder="选择日期" style="width: 240px" />
             </el-form-item>
          <el-form-item>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
             <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
          </el-form-item>
       </el-form>
       <!-- 表格数据 -->
       <el-table v-loading="loading" :data="roleList">
         <el-table-column label="" prop="id" width="100" >
            <template #default="scope">
                <span class="idx"  :style="{ backgroundColor: scope.$index < 3 ? 'black' : 'gray' }">
                  {{scope.$index + 1}}
                </span>
            </template>
          </el-table-column>
          <el-table-column label="消息内容" prop="ex"  width="150" :show-overflow-tooltip="true">
            <template #default="scope">
               <span>
                  {{scope.row.chatLog.ex === '' ? '-' : scope.row.chatLog.ex}}
               </span>
            </template>
          </el-table-column>
          <el-table-column label="发送者昵称" width="150">
            <template #default="scope">
               <span>
                  {{scope.row.chatLog.senderNickname}}
               </span>
            </template>
          </el-table-column>
          <el-table-column label="发送者ID" >
            <template #default="scope">
               <span>
                  {{scope.row.chatLog.sendID}}
               </span>
            </template>
          </el-table-column>
          <el-table-column label="接受者ID">
            <template #default="scope">
               <span>
                  {{scope.row.chatLog.recvID}}
               </span>
            </template>
          </el-table-column>
          <el-table-column label="会话类型">
            <template #default="scope">
               <span>
                  {{sessionTypeMap[scope.row.chatLog.sessionType]}}
               </span>
            </template>
          </el-table-column>
          <el-table-column label="消息类型">
            <template #default="scope">
               <span>
                  {{contentTypeMap[scope.row.chatLog.contentType]}}
               </span>
            </template>
          </el-table-column>
          <el-table-column label="发送时间">
            <template #default="scope">
              <span>{{ parseTime(scope.row.chatLog.sendTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="" align="center" class-name="small-padding fixed-width">
             <template #default="scope">
                 <el-popconfirm   
                    confirmButtonText="确定"
                    cancelButtonText="取消"
                    title="确认要撤回该条消息吗？"
                    @confirm="handleBack(scope.row)"
                 >
                    <template #reference>
                        <!-- <el-button link type="primary">撤回</el-button> -->
                        <span>撤回</span>
                    </template>
                 </el-popconfirm>
             </template>
          </el-table-column>
       </el-table>
 
       <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="pageObj.pageNumber"
          v-model:limit="pageObj.showNumber"
          @pagination="getList"
       />
        <el-drawer size="20%" :title="title" v-model="openDrawer" direction="rtl" :before-close="handleClose" destroy-on-close>
          <el-form ref="roleRef" :model="form" :rules="rules" label-width="0px">
             <el-form-item label="" prop="roleName" v-show="single">
                <div class="radioContainer">
                    <el-radio v-model="radio1" label="1">普通群员</el-radio>
                    <el-radio v-model="radio1" label="2">群管理员</el-radio>
                    <el-radio v-model="radio1" label="3">群主</el-radio>
                </div>
             </el-form-item>
          </el-form>
          <el-form ref="roleRef" :model="form" :rules="rules" label-width="0px">
             <el-form-item label="" prop="roleName" v-show="!single">
                <div class="radioContainer">
                    <el-radio v-model="radio2" label="1">10分钟</el-radio>
                    <el-radio v-model="radio2" label="2">1小时</el-radio>
                    <el-radio v-model="radio2" label="3">12小时</el-radio>
                    <el-radio v-model="radio2" label="4">1天</el-radio>
                </div>
             </el-form-item>
             <div  class="customTime" v-show="!single">自定义时长</div>
             <el-form-item label="" prop="timer" v-show="!single">
                <el-input v-model="form.timer" />
             </el-form-item>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
             </div>
          </template>
       </el-drawer>
    </div>
</template>
<script setup name="userMessage">
import { ref, reactive, onMounted, onBeforeUnmount } from 'vue'
import { getMessageList } from "@/api/im/message";
import { formatDate } from "@/utils";
import { sessionTypeOptions, contentTypeOptions, contentTypeMap, sessionTypeMap } from '@/constant'
 const router = useRouter();
 const { proxy } = getCurrentInstance();
 
 const roleList = ref([]);
 const loading = ref(true);
 const showSearch = ref(true);
 const single = ref(true);
 const total = ref(0);
 const title = ref("");
 const dateRange = ref([]);
 const data = reactive({
   form: {},
   queryParams: {
     sendID: '',
     recvID: '',
     sessionType: 1,
     contentType: 0,
     sendTime: formatDate(new Date(), 'yyyy-MM-dd'),
   },
   pageObj:{
      pageNumber: 1,
      showNumber: 10
   },
   rules: {
     groupName: [{ required: true, message: "群组名称不能为空", trigger: "blur" }],
   },
 });
 
 const { queryParams, form, rules, pageObj } = toRefs(data);
  
 /** 查询角色列表 */
 function getList() {
   loading.value = true;
   const params={
      pagination: data.pageObj,
      ...data.queryParams
   }
   getMessageList(params).then(response => {
     loading.value = false;
     roleList.value = response.data.chatLogs;
     total.value = response.data.total;
   });
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   handleQuery();
 }
 getList();
</script>